<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证码</title>
    <style>
        svg{
            width: 100px;
            height:40px;
            border:1px solid red;
        }
    </style>
</head>
<body>
    <svg id="svg" xmlns="http://www.w3.org/2000/svg" version="1.1">
    </svg>
    <script>
        // 封装产生随机数的方法
        function random(start,end){
            if(!end){
                return parseInt(Math.random() * start);
            }
            return parseInt(Math.random() * (end - start + 1) + start);
        }
        let svg = document.getElementById("svg");
        let str = "0123456789abcdefghijklmnopqrstuvwxyz";
        let codeStr = "";
        for(let i = 0;i < 4;i++){
            let code = str[random(str.length)];
            let x = i * 20 + random(15);
            let y = random(10,40);
            console.log("x,y:",x,y);
            // codeStr += `<text x="-15" y="10"
            //     style="font-size:30px;fill:rgb(${random(256)},${random(256)},${random(256)});
            //     transform:translate(${x + 15}px,${y - 10}px) rotate(${random(-45,45)}deg)"
            //     >${code}</text>`;
            codeStr += `<tspan x="${x}" y="${y}"
                rotate="${random(-45,45)}"
                style="font-size:30px;fill:rgb(${random(256)},${random(256)},${random(256)});"
                >${code}</tspan>`;
        }
        codeStr = "<text>"+codeStr+"</text>"
        for(let i = 0;i < 5;i++){
            codeStr += `<line x1="${random(100)}" y1="${random(40)}" x2="${random(100)}" y2="${random(40)}"
                stroke-width="${random(1,3)}"
                stroke="rgb(${random(256)},${random(256)},${random(256)})"
            ></line>`;
        }
        
        svg.innerHTML = codeStr;
    </script>
</body>
</html>